<?php
class Admin_Model_Kiemtra{
	
	private $exame;
	
	public function __construct(){
		$this->exame = new Admin_Model_DbTable_Kiemtras();
	}
	
	public function listKiemTra($where=NULL){
		if($where!=NULL){
			return $this->exame->select()->where($where);
		}else {
			return  $this->exame->select();
		}
	}
	
	function selectManager_lopKiemTra($where =null,$order = NULL,$limit =NULL){
		$dbSelect = new Zend_Db_Select(Zend_Db_Table::getDefaultAdapter());
		$dbSelect->from(array('kt'=>'kiemtra'),'*')
				->joinLeft(array('lkt'=>'lop-kiemtra'), 'kt.idKiemTra = lkt.idKiemTra','idLop')
				->joinLeft(array('l'=>'lop'), 'l.id = lkt.idLop',array('tenLop','khoa'))
				->joinLeft(array('ql'=>'quanly'), 'ql.idLop = l.id',array('username','idMonHoc'));
	
		if ($where != NULL) $dbSelect->where($where);
		if ($order != NULL) $dbSelect->order($order);
		if ($limit != NULL) $dbSelect->limit($limit);
		
		return $dbSelect;
	}
	
	/**
	 * 
	 * Them bai kiem tra
	 * @param unknown_type $name : ten cho bai kiem tra
	 * @param unknown_type $time: thoi gian lam bai
	 * @param unknown_type $numQuestion : so cau hoi trong bai lam
	 * @param unknown_type $dateStart: ngay bat dau lam
	 * @param unknown_type $hourStart: gio bat dau lam
	 * @param unknown_type $dateEnd :  ngay ket thuc
	 * @param unknown_type $hourEnd : gio ket thuc
	 * @param unknown_type $idMonHoc :user
	 * @param unknown_type $userName
	 */
	public function addKiemTra($name, $time, $numQuestion, $dateStart, $hourStart,
	 				$dateEnd, $hourEnd, $idMonHoc,$heSo,$hocKy,$namHoc){

	 	$data = array(
	 		'ten'		=>	$name,
	 		'thoiGian'	=>	$time,
	 		'soCauHoi'	=>	$numQuestion,
	 		'dateStart'	=>	$dateStart,
	 		'hourStart'	=>	$hourStart,
	 		'dateEnd'	=>	$dateEnd,
	 		'hourEnd'	=>	$hourEnd,
	 		'idMonHoc'	=>	$idMonHoc,
	 		'heSo'		=>	$heSo,
	 		'hocKy'		=>	$hocKy,
	 		'namHoc'	=>	$namHoc
	 	);
		$this->exame->insert($data);	 
	}
	
	/**
	 * 
	 * Cap nhap bai kiem tra
	 * @param unknown_type $name : ten cho bai kiem tra
	 * @param unknown_type $time: thoi gian lam bai
	 * @param unknown_type $numQuestion : so cau hoi trong bai lam
	 * @param unknown_type $dateStart: ngay bat dau lam
	 * @param unknown_type $hourStart: gio bat dau lam
	 * @param unknown_type $dateEnd :  ngay ket thuc
	 * @param unknown_type $hourEnd : gio ket thuc
	 * @param unknown_type $idMonHoc :user
	 * @param unknown_type $userName
	 * @param unknown_type $idKiemTra
	 */
	public function updateKiemTra($name, $time, $numQuestion, $dateStart, $hourStart,
	 				$dateEnd, $hourEnd, $hocKy,$namHoc, $idMonHoc, $idKiemTra){
	 					
		$data = array(
	 		'ten'		=>	$name,
	 		'thoiGian'	=>	$time,
	 		'soCauHoi'	=>	$numQuestion,
	 		'dateStart'	=>	$dateStart,
	 		'hourStart'	=>	$hourStart,
	 		'dateEnd'	=>	$dateEnd,
	 		'hourEnd'	=>	$hourEnd,
			'hocKy'		=>	$hocKy,
			'namHoc'	=>	$namHoc,
	 		'idMonHoc'	=>	$idMonHoc,
	 	);
	 	
	 	$this->exame->update($data, 'idKiemTra ='.$idKiemTra);
	 	
	}
	
	public function deleteKiemTra($idKiemTra){
		$this->exame->delete('idKiemTra = '.$idKiemTra);
	}
	
	public function update($data,$where){
		$this->exame->update($data, $where);
	}
	
	public function fetchKiemTra($id){
		return $this->exame->fetchRow('idKiemTra = '.$id);
	}
	
	public function fetchAllKiemTra($where=NULL){
		return $this->exame->fetchAll($where);
	}
	
	/**
	 * 
	 * Get he so cua bai kiem tra
	 * @return Int
	 */
	public function getHeSoKiemTra($id){
		$heso = $this->exame->fetchRow('idKiemTra ='.$id);
		
		return $heso['heSo'];
	}
	
	public function getIdMonHoc($idKiemTra){
		$mon = $this->exame->fetchRow('idKiemTra ='.$idKiemTra);
		
		return $mon['idMonHoc'];
	}
	
	/**
	 * 
	 * Lay 1 dong trong table kiem tra
	 * @param unknown_type $recordName: String: ten cot trong bang du lieu
	 * 
	 * @return mixe
	 */
	public function getRecord($recordName, $idKiemTra){
		$record = $this->exame->fetchRow('idKiemTra ='.$idKiemTra);
		
		return $record[$recordName];
	}
	
	public function getMaxIdExame(){
		$exame = $this->exame->getAdapter()
				->query("select max(idKiemTra) as idKiemTra from kiemtra")
				->fetchAll();
		return $exame[0]['idKiemTra'];
	}
}